package com.lhf.springcloud.controller.teacher;

import com.lhf.springcloud.Entity.teacher.TeacherBasicData;
import com.lhf.springcloud.service.TeacherBasicDataService;
import com.lhf.springcloud.utils.ExcelUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 老师接口
* */
@Controller
@RequestMapping("/teacherData")
public class TeacherBasicController {
    @Autowired
    private TeacherBasicDataService teacherBasicDataService;

    /**
     * 页面跳转
     */
    @GetMapping("/teacherBasicData")
    public String teacherBasicData(){
        return "project/dataCenter/teacher/teacherBasicData";
    }
    @RequestMapping(value = "/findAllTeacherBasicData",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询学生基础数据")
    public Map findAllData(Integer page,Integer limit){
        if (page==null){
            page=1;
        }
        if (limit==null){
            limit=10;
        }
        page=(page-1)*limit;
        Map allTeacherBasicData = teacherBasicDataService.findAllTeacherBasicData(page, limit);
        Map map=new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",allTeacherBasicData.get("count"));
        map.put("data",allTeacherBasicData.get("data"));
        return map;
    }
    @RequestMapping(value = "/findByIdCard",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("通过身份证号查询学生信息")
    @ApiImplicitParam(name = "idCard",value = "教职工身份证号")
    public Map findByIdCard(String idCard,String teacherName,Integer page,Integer limit){
        if (page==null){
            page=1;
        }
        if (limit==null){
            limit=10;
        }
        page=(page-1)*limit;
        Map byIdCard = teacherBasicDataService.findByIdCard(idCard, teacherName, page, limit);
        Map map=new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",byIdCard.get("count"));
        map.put("data",byIdCard.get("data"));
        return map;
    }
    @RequestMapping(value = "/updateTeacherBasicData",method = RequestMethod.GET)
    @ApiOperation("根据id更改字段值")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "要更改字段的id"),
            @ApiImplicitParam(name = "value",value = "需要更新的新值"),
            @ApiImplicitParam(name = "field",value ="要更新的字段名" ),})
    public String updateData(String value,String id,String field){
        teacherBasicDataService.updateData(value,id,field);

        return "redirect:/studentBasicData";
    }
    @GetMapping("/studentBasicFiles")
    @ResponseBody
    @ApiOperation("文件导出")
    public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
        // 下载本地文件
        String fileName = "教职工基础数据表.xlsx".toString(); // 文件的默认保存名
        // 读到流中
        String property = System.getProperty("user.dir");
//        InputStream inStream = new FileInputStream("http://192.168.7.22:888/project/excel/学生基础数据表.xlsx");// 文件的存放路径
        InputStream inStream = new FileInputStream(property+"/excel/教职工基础数据表.xlsx");// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("bin");
        try {
            response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) );
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //文件上传
    @PostMapping("/upLoadTeacherBasicInfo")
    @ApiOperation("文件上传")
    @ApiImplicitParam(name = "file",value = "要上传的文件")
    public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes){
        if(file.isEmpty()) {
            return "redirect:/teacherData/teacherBasicData";
        }
        List<TeacherBasicData> list1= new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = ExcelUtils.getCourseListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();

            for (int i = 0; i < list.size(); i++) {
                List<Object> courseList = list.get(i);

                TeacherBasicData course = new TeacherBasicData();
                if (courseList.size()==0){
                    break;
                }
                course.setTeacherName(courseList.get(0).toString());
                course.setIdCard(courseList.get(1).toString());
                course.setBirthday(courseList.get(2).toString());
                course.setStatus(courseList.get(3).toString());
                list1.add(course);
                // 执行插入操作

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list1.size()>0){
            teacherBasicDataService.addCourse(list1);
        }
        //redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!" );

        return "redirect:/teacherData/teacherBasicData";  //重定向
    }

}
